import { Component, OnInit } from "@angular/core";
import { AxiosService } from "../../axios.service";
import { DatePipe } from "@angular/common";
import { SSL_OP_SSLEAY_080_CLIENT_DH_BUG } from "constants";

@Component({
  selector: "app-lirun",
  templateUrl: "./lirun.component.html",
  styleUrls: ["./lirun.component.scss"],
  providers: [DatePipe],
})
export class LirunComponent implements OnInit {
  month1;
  month2;
  week='';

  dlhours='';
  idlhours='';
  other='';

  dlperson='';
  dlhumancost='';
  dlovertime='';
  idlperson='';
  idlhumancost='';
  idlovertime='';
  depreciation='';
  rent='';
  sdcharge='';


  listOfData;
  isVisible;
  widthConfig=[
    "100px","80px","110px","110px","110px","130px","130px","110px","140px","140px","140px","110px","110px","100px",
  ]


  constructor(public axios: AxiosService, private datePipe: DatePipe) {}

  ngOnInit(): void {
    this.queryData();
  }
  //  查询（从后台返回的数组参数）
  queryData() {
    this.axios.axiosPost("QueryYunyingCostInfo",{month:this.month1})
      .then((data) => {
        if (data) {
          console.log(data);
          this.listOfData = data["order"];
          console.log(this.listOfData);
          // console.log(this.listOfData[1].data[0]['name']);
          for(let i=0;i<this.listOfData.length;i++){
            if(this.listOfData[i].week==""){
              this.listOfData[i].data.unshift({name:"dlhours",value:""},
                                              {name:"idlhours",value:""},
                                              {name:"other",value:""})
            }else{
              let empty=[
                                              // {name:"dlhours",value:""},
                                              // {name:"idlhours",value:""},
                                              // {name:"other",value:""},
                {name:"dlperson",value:""},
                {name:"dlhumancost",value:""},
                {name:"dlovertime",value:""},
                {name:"idlperson",value:""},
                {name:"idlhumancost",value:""},
                {name:"idlovertime",value:""},
                {name:"depreciation",value:""},
                {name:"rent",value:""},
                {name:"sdcharge",value:""},
            ]
            this.listOfData[i].data=this.listOfData[i].data.concat(empty)
            }
          }

          console.log(this.listOfData);
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }

  //  新增/修改
  edit(data) {
    // debugger
    if(data.week!=""){
      this.month1=data.month;
      this.month2="";
      this.week=data.week;
    }else{
      this.month2=data.month;
      this.month1="";
      this.week=null;
      console.log(this.week)
    }

    this.dlhours = data.data[0]['value'];
    this.idlhours = data.data[1]['value'];
    this.other = data.data[2]['value'];
    this.dlperson = data.data[3]['value'];
    this.dlhumancost = data.data[4]['value'];
    this.dlovertime = data.data[5]['value'];
    this.idlperson = data.data[6]['value'];
    this.idlhumancost = data.data[7]['value'];
    this.idlovertime = data.data[8]['value'];
    this.depreciation = data.data[9]['value'];
    this.rent = data.data[10]['value'];
    this.sdcharge = data.data[11]['value'];
    console.log(typeof(this.week) )
  //
}

  //  清除信息按钮(周)
  clear1() {
    this.month1 = "";
    this.week = null;
    this.dlhours = "";
    this.idlhours = "";
    this.other = "";
  }

  //  清除信息按钮(周)
  clear2() {
    this.month2 = "";
    this.dlperson = "";
    this.dlhumancost = "";
    this.dlovertime = "";
    this.idlperson = "";
    this.idlhumancost = "";
    this.idlovertime = "";
    this.depreciation = "";
    this.rent = "";
    this.sdcharge = "";
  }

  //  保存按钮（传给后台的参数）
  save1() {
    console.log(typeof(this.week) )
    // console.log(this.month1);
    if(this.month1!=undefined&&this.week!=""&&this.dlhours!=""&&this.idlhours!=""&&this.other!=""){
      this.axios.axiosPost("UpdateYunyingCostInfo", {
        month: this.month1,
        week: this.week,
        dlhours: this.dlhours,
        idlhours: this.idlhours,
        other: this.other,
      })
      .then((data) => {
        if (data) {
          console.log(data);
          console.log(this.month1);
          console.log(this.listOfData[0].data[0]['value'])
          this.clear1();
          this.queryData();

        }
      })
      .catch((err) => {
        console.log(err);
      });
    }else{
      alert("任何數據都不可為空")
    }

  }
  save2() {
    // debugger
    if(this.month2!=undefined&&this.dlperson!=""&&this.dlhumancost!=""&&this.dlovertime!=""&&this.idlperson!=""&&this.idlhumancost!=""&&this.idlovertime!=""&&this.depreciation!=""&&this.rent!=""&&this.sdcharge!=""){
      this.axios.axiosPost("UpdateYunyingCostInfo", {
        month: this.month2,
        dlperson: this.dlperson,
        dlhumancost: this.dlhumancost,
        dlovertime: this.dlovertime,
        idlperson: this.idlperson,
        idlhumancost: this.idlhumancost,
        idlovertime: this.idlovertime,
        depreciation: this.depreciation,
        rent: this.rent,
        sdcharge: this.sdcharge
      })
      .then((data) => {
        if (data) {
          console.log(data);
          this.clear2();
          this.queryData();

        }
      })
      .catch((err) => {
        console.log(err);
      });
    }else{
      alert("任何數據都不可為空")
    }

  }

  showModal(data) {
    this.isVisible = true;
  }

  handleCancel() {
    this.isVisible = false;
  }

  // 时间处理
  TimeChange1(date: Date): void {
    this.month1 = date;
    this.month1 = this.datePipe.transform(this.month1, "yyyy/MM");
    console.log(this.month1);
  }
  TimeChange2(date: Date): void {
    this.month2 = date;
    this.month2 = this.datePipe.transform(this.month2, "yyyy/MM");
    console.log(this.month2);
  }
}
